﻿package map 
{
	import Common.GameDefine;
	import Common.MapDirection;
	
	/**
	 * ...
	 * @author fox
	 */
	public class MapGrid 
	{
		public static const gridX:int = 64;
		public static const gridY:int = 48;
		public static const gridInclined:int = 40;
		
		public static const gridInclinedHalf:int = 20;
		public static const gridXHalf:int = 32;
		public static const gridYHalf:int = 24;
		public static const gridXHalfHalf:int = 16;
		public static const gridYHalfHalf:int = 12;
		
		public static const gridPX:Number = 0.8;
		public static const gridPY:Number = 0.6;
		
		public static const gridXFour:int = 16;
		public static const gridYFour:int = 12;
		//public static const PX:Number = 30 * gridX * 0.25;
		//public static const PY:Number = 30 * gridY * 0.25;
		
		
		//public var arround:Array = null;
		
		public var x:int = GameDefine.INVALIDID;
		public var y:int = GameDefine.INVALIDID;
		
		public var realPosX:Number = 0.0;
		public var realPosY:Number = 0.0;
		
		public var posX:Number = 0.0;
		public var posY:Number = 0.0;
		
		public function copy( grid:MapGrid ):void
		{
			this.x = grid.x;
			this.y = grid.y;
			
			realPosX = grid.realPosX;
			realPosY = grid.realPosY;
		}
		
		public function check():Boolean
		{
			return realPosX == x * gridXHalf + y * gridXHalf && realPosY == -x * gridYHalf + y * gridYHalf;
		}
		
		public function equalsReal( compare:MapGrid ):Boolean 
		{
			return compare.realPosX == realPosX && compare.realPosY == realPosY;
		}
		
		public function equals( compare:MapGrid ):Boolean 
		{
			return compare.x == x && compare.y == y;
		}
		
		public function move( direct:int ):void
		{
			switch( direct )
			{
			case MapDirection.EAST:
				x++;
				setValue( x , y );
				break;
			case MapDirection.SOUTH:
				y++;
				setValue( x , y );
				break;
			case MapDirection.WEST:
				x--;
				setValue( x , y );
				break;
			case MapDirection.NORTH:
				y--;
				setValue( x , y );
				break;
			case MapDirection.NORTHEAST:
				x++;y--;
				setValue( x , y );
				break;
			case MapDirection.NORTHWEST:
				x--;y--;
				setValue( x , y );
				break;
			case MapDirection.SOUTHEAST:
				x++;y++;
				setValue( x , y );
				break;
			case MapDirection.SOUTHWEST:
				x--;y++;
				setValue( x , y );
				break;
			}
		}
		
		public function isAround( compare:MapGrid ):Boolean
		{
			if ( compare.x == x + 1 && compare.y == y ) 
			{
				return true;
			}
			
			if ( compare.x == x - 1 && compare.y == y ) 
			{
				return true;
			}
			
			if ( compare.x == x && compare.y == y + 1 ) 
			{
				return true;
			}
			
			if ( compare.x == x && compare.y == y - 1 ) 
			{
				return true;
			}
			
			if ( compare.x == x - 1 && compare.y == y - 1 ) 
			{
				return true;
			}
			
			if ( compare.x == x + 1 && compare.y == y - 1 ) 
			{
				return true;
			}
			
			if ( compare.x == x - 1 && compare.y == y + 1 ) 
			{
				return true;
			}
			
			if ( compare.x == x + 1 && compare.y == y + 1 ) 
			{
				return true;
			}
			
			return false;
		}
		
		public function MapGrid( x:int = 0 , y:int = 0 ) 
		{
			setValue( x , y );
		}
		
		public function setValue( x:int = 0 , y:int = 0 ):void
		{
			this.x = x;
			this.y = y;
			
			realPosX = x * gridXHalf + y * gridXHalf;
			realPosY = -x * gridYHalf + y * gridYHalf;
			
			posX = realPosX;
			posY = realPosY;
		}
		
		public function clone():MapGrid 
		{
			return new MapGrid( x , y );
		}
		
		public function toString():String 
		{
			var ss:String = "<x= " + x + " ,y= " + y;
			ss += "  realX= " + realPosX + " ,realY= " + realPosY + " >";
			return ss;
		}
		
	}
	
}